package com.ac.scum.log.task.face.log.counsumer;

import com.ac.scum.log.bean.PlayerAlarm;
import com.ac.scum.log.bean.ServerAlarmConfig;
import com.ac.scum.log.bean.ServerSysConfig;
import com.ac.scum.log.task.mapper.pack.PackageMapper;
import com.ac.scum.log.task.mapper.player.PlayerAlarmMapper;
import com.ac.scum.log.task.mapper.server.ServerAlarmConfigMapper;
import com.ac.scum.log.task.mapper.server.ServerSysConfigMapper;
import com.ac.scum.log.task.utils.queue.FTPQueueItem;
import com.ac.scum.log.task.utils.spring.SpringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;

import java.util.List;

public interface PlayerAlarmInterface {

    default ServerAlarmConfig getCurrentAlarmConfig(FTPQueueItem ftpQueueItem) {
        Long serverId = ftpQueueItem.getServer().getId();
        Boolean hasPackage = SpringUtils.getBean(PackageMapper.class).serverHasPackage(serverId, 3L);
        ServerAlarmConfig serverAlarmConfig = new ServerAlarmConfig();
        serverAlarmConfig.setExplosivesEnabled(false);
        serverAlarmConfig.setChestEnabled(false);
        serverAlarmConfig.setFlagEnabled(false);
        serverAlarmConfig.setLockEnabled(false);
        serverAlarmConfig.setTrapEnabled(false);

        if (hasPackage == null || !hasPackage) {
            return serverAlarmConfig;
        }
        ServerSysConfig serverSysConfig = SpringUtils.getBean(ServerSysConfigMapper.class).selectOne(Wrappers.lambdaQuery(ServerSysConfig.class).eq(ServerSysConfig::getServerId, serverId));
        if (serverSysConfig == null || !serverSysConfig.getEnableAlarm()) {
            return serverAlarmConfig;
        }
        return SpringUtils.getBean(ServerAlarmConfigMapper.class).selectOne(Wrappers.lambdaQuery(ServerAlarmConfig.class).eq(ServerAlarmConfig::getServerId, serverId));
    }

    default List<Long> getHasAlarmPackagePlayerIds(FTPQueueItem ftpQueueItem) {
        Long serverId = ftpQueueItem.getServer().getId();
        Long packageId = 1796048740967038977L;
        PackageMapper packageMapper = SpringUtils.getBean(PackageMapper.class);
        Boolean hasPackage = packageMapper.serverHasPackage(serverId, packageId);
        if (hasPackage == null || !hasPackage) {
            return Lists.newArrayList();
        }
        return packageMapper.getHasPackagePlayerIds(serverId, packageId);
    }

    default void commitAlarmTask(List<PlayerAlarm> playerAlarms) {
        if (playerAlarms != null && !playerAlarms.isEmpty()) {
            PlayerAlarmMapper playerAlarmMapper = SpringUtils.getBean(PlayerAlarmMapper.class);
            playerAlarms.forEach(playerAlarmMapper::insert);
        }
    }
}
